home *** CD-ROM | disk | FTP | other *** search
/ Internet Surfer: Getting Started / Internet Surfer - Getting Started (Wayzata Technology)(7231)(1995).bin / pc / mac / bonus / peter_le / finger-1 / my_units / mynotifi.uni < prev    next >
Text File  |  1992-02-24  |  5KB  |  213 lines

  1. unit MyNotifier;
  2.  
  3. { This code is part of the Finger/Fingerd source code, written in THINK Pascal 4 }
  4. { Copyright 1991-1992 Peter N Lewis }
  5. { This code is public domain.  Do with it as you like }
  6. { This is from my library of useful routines }
  7.  
  8. { Derived from <jholt@adobe.COM> Joe Holt's StartupError code as posted }
  9. { to comp.sys.mac.programmer in May 1991 }
  10.  
  11. { Notification Manager messages }
  12.  
  13. { History: }
  14. {   jhh 18 jun 90 -- response to news posting }
  15. {   pnl 29 may 91 -- Converted to pascal to be used in an application }
  16.  
  17. interface
  18.  
  19.     const
  20.         mark_app = 1;
  21.         mark_none = 0;
  22.  
  23.     procedure InitNotify;
  24.     procedure FinishNotify;
  25.     procedure NotifyH (mark: integer; sound: handle; sicn: handle; str: stringPtr);
  26.     procedure Notify (mark, sound: boolean; sicn_id, sicn_index, str_id, str_index: integer);
  27. { mark - mark the current application }
  28. { sound - play sysbeep }
  29. { sicn_id, sicn_index - SICN id to rotate with the apple & index (<1 -> 1)   OR 0&0 for no sicn }
  30. { str_id, str_index - STR# id & index    OR    STR id & 0    OR    0 & 0 }
  31.     procedure NotifyCompletion;
  32. { Call this when in event loop when in the foreground to remove apple mark and flash }
  33.     procedure UnNotify;
  34. { Call this to get rid of the notification }
  35.  
  36.     var
  37.         notify_finished: boolean;
  38.  
  39. implementation
  40.  
  41.     uses
  42.         Notification;
  43.  
  44.     const
  45.         sicn_size = 32;
  46.         T_NMInstall = $A05E;
  47.         T_Unimplemented = $A89F;
  48.  
  49.     type
  50.         NMRecPtrPtr = ^NMRecPtr;
  51.         booleanPtr = ^boolean;
  52.  
  53.     var
  54.         current_note: NMRecPtr;
  55.  
  56. { handles must be non-purgeable, but may be unlocked }
  57.  
  58. {$S Init}
  59.     procedure InitNotify;
  60.     begin
  61.         current_note := nil;
  62.         notify_finished := false;
  63.     end;
  64.  
  65. {$S}
  66.     procedure MyResponse (note: NMRecPtr);
  67.     begin
  68.         booleanPtr(note^.nmRefCon)^ := true;
  69.     end;
  70.  
  71. {$S Util}
  72.     procedure UnNotify;
  73.         var
  74.             oe: OSErr;
  75.     begin
  76.         if current_note <> nil then begin
  77.             oe := NMRemove(current_note);
  78.             with current_note^ do begin
  79.                 if nmStr <> nil then
  80.                     DisposPtr(pointer(nmStr));
  81.                 if nmIcon <> nil then
  82.                     DisposHandle(nmIcon);
  83.             end;
  84.             DisposPtr(pointer(current_note));
  85.             current_note := nil;
  86.         end;
  87.         notify_finished := false;
  88.     end;
  89.  
  90. {$S}
  91.     procedure NotifyCompletion;
  92.     begin
  93.         if notify_finished then
  94.             UnNotify;
  95.     end;
  96.  
  97. {$S Term}
  98.     procedure FinishNotify;
  99.     begin
  100.         if current_note <> nil then
  101.             UnNotify;
  102.     end;
  103.  
  104. {$S Util}
  105.     procedure NotifyH (mark: integer; sound: handle; sicn: handle; str: stringPtr);
  106.         var
  107.             errorText: str255;
  108.             sh: stringHandle;
  109.             sicnH: handle;
  110.             error: boolean;
  111.             oe: OSErr;
  112.     begin
  113.         UnNotify;            { Clear outstanding notify }
  114.         if NGetTrapAddress(T_NMInstall, OSTrap) = NGetTrapAddress(T_Unimplemented, ToolTrap) then begin
  115.             SysBeep(1);   { Best we can do I guess.  Could put up the dialog box maybe?...}
  116.         end
  117.         else begin
  118.             current_note := NMRecPtr(NewPtr(sizeof(NMRec)));
  119.             if current_note = nil then begin
  120.                 SysBeep(1);   { Can't do much else if there isnt even room for this! }
  121.             end
  122.             else begin
  123.                 with current_note^ do begin
  124.                     qType := nmType;
  125.                     error := false;
  126.                     booleanPtr(nmRefCon) := @notify_finished;
  127.                     nmMark := mark;
  128.                     nmStr := str;
  129.                     nmIcon := sicn;
  130.                     nmSound := sound;
  131.                     nmResp := @MyResponse;
  132.                 end;
  133.                 oe := NMInstall(current_note);
  134.                 if oe <> noErr then begin
  135.                     current_note := nil;
  136.                     SysBeep(1);
  137.                 end;
  138.             end;
  139.         end;
  140.     end;
  141.  
  142. {$S Util}
  143.     procedure Notify (mark, sound: boolean; sicn_id, sicn_index, str_id, str_index: integer);
  144.         var
  145.             errorText: str255;
  146.             sh: stringHandle;
  147.             sicnH: handle;
  148.             error: boolean;
  149.             oe: OSErr;
  150.             nmMark: integer;
  151.             nmStr: stringPtr;
  152.             nmIcon: handle;
  153.             nmSound: handle;
  154.     begin
  155.         error := false;
  156.         if mark then
  157.             nmMark := 1
  158.         else
  159.             nmMark := 0;
  160.         nmStr := nil;
  161.         if str_id <> 0 then begin
  162.             if str_index > 0 then
  163.                 GetIndString(errorText, str_id, str_index)
  164.             else begin
  165.                 errorText := '';
  166.                 sh := GetString(str_id);
  167.                 if sh <> nil then begin
  168.                     if sh^ <> nil then
  169.                         errorText := sh^^;
  170.                     ReleaseResource(handle(sh));
  171.                 end;
  172.             end;
  173.             if errorText = '' then
  174.                 error := true
  175.             else begin
  176.                 nmStr := stringPtr(NewPtr(length(errorText) + 1));
  177.                 if nmStr = nil then
  178.                     error := true
  179.                 else
  180.                     nmStr^ := errorText;
  181.             end;
  182.         end;
  183.         nmIcon := nil;
  184.         if sicn_id <> 0 then begin
  185.             if sicn_index < 1 then
  186.                 sicn_index := 1;
  187.             sicn_index := (sicn_index - 1) * sicn_size;   { 1-based, like STR# }
  188.             sicnH := GetResource('SICN', sicn_id);
  189.             HNoPurge(sicnH);
  190.             if sicnH = nil then
  191.                 error := true
  192.             else begin
  193.                 nmIcon := NewHandle(sicn_size);
  194.                 if nmIcon = nil then
  195.                     error := true
  196.                 else if nmIcon^ = nil then
  197.                     error := true
  198.                 else if GetHandleSize(sicnH) < sicn_index + sicn_size then
  199.                     error := true
  200.                 else begin
  201.                     BlockMove(ptr(longInt(sicnH^) + sicn_index), nmIcon^, sicn_size);
  202.                 end;
  203.                 ReleaseResource(sicnH);
  204.             end;
  205.         end;
  206.         if sound or error then
  207.             nmSound := handle(-1)
  208.         else
  209.             nmSound := nil;
  210.         NotifyH(nmMark, nmSound, nmIcon, nmStr);
  211.     end;
  212.  
  213. end.